<!--
 * @Author: cl
 * @Description: 登录
 * @Date: 2023-04-07 16:15:45
 * @LastEditTime: 2023-05-10 15:51:30
 * @FilePath: \vue3\src\views\login\index.vue
 * @LastEditors: cl
-->
<template>
  <div class="wraps">
    <div class="form-css" v-loading="loading">
      <el-form label-width="80px">
        <el-form-item prop="name" label="用户名">
          <el-input v-model="form.name" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item prop="" label="">
          <el-button
            type="primary"
            @click="userLogin"
            color="#458499"
            style="width: 300px"
          >
            登录
          </el-button>
        </el-form-item>
        <el-form-item prop="" label="">
          <el-button
            type="primary"
            @click="userRegister"
            color="#458499"
            style="width: 300px"
          >
            注册
          </el-button>
        </el-form-item>
        <el-form-item prop="" label="">
          <el-button
            type="primary"
            @click="loginOut"
            color="#458499"
            style="width: 300px"
          >
            退出登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts">
import { register } from "@/api/moudules/user";
import { userStore } from "../../store/user";
import { setToken, removeToken } from "../../utils/auth";
import { ElMessage } from "element-plus";
const mystore = userStore();
const router = useRouter();

const toNextPage = () => {
  // router.push({name: "score",params: { id: 123,age:999 },query:{sex:'nan'}});
  // router.push({path: "/nestjs/score/2",params: { id: 123,age:999 },query:{sex:'nan'}});
  router.push({ path: "/nestjs/score/321", query: { id: 123 } });
};
//表单
const form = reactive({
  name: "",
  password: "",
});
let loading = ref(false);
//清空数据
const resetForm = reactive({ ...form });
//登录
interface netInfo {
  data: any;
  message: string;
  status: number;
}
const userLogin = () => {
  loading.value = true;
  mystore
    .userlogin(form)
    .then((res: any) => {
      if (res.status == 0) {
        setToken(res.data);
        router.push({ path: "/", replace: true });
      } else {
        ElMessage.error(res.message);
        loading.value = false;
      }
    })
    .finally(() => {
      loading.value = false;
    });
};
const userRegister = () => {
  register(form).then((res: netInfo) => {
    if (res.status != 0) {
      ElMessage.error(res.message);
    }
  });
};
const loginOut = () => {
  removeToken();
};
</script>

<style scoped>
html,
body {
  background: #ccc;
  height: 100vh;
}

.wraps {
  height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.form-css {
  width: 400px;
  height: 300px;
  border: 1px solid grey;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  align-content: flex-start;
  background-color: rgba(25, 115, 151, 0.3);
  padding-right: 50px;
}
</style>
